<?php

namespace app\data\controller\api\auth;

use app\data\controller\api\Auth;
use app\data\service\AliMessageService;

/**
 * 用户银行卡管理
 * Class Bank
 * @package app\data\controller\api\auth
 */
class Bank extends Auth
{
    /**
     * 绑定数据表
     * @var string
     */
    private $table = 'DataUserBank';

    /**
     * 添加银行卡
     * @throws \think\db\exception\DbException
     */
    public function set()
    {
        $data = $this->_vali([
            'uuid.value'       => $this->uuid,
            'bank_card_number.require' => '请输入银行卡号',
            'bank_name.require' => '请输入银行名称',
            'sub_branch_name.require' => '请输入支行名称',
            'province.require' => '请输入银行卡省份',
            'city.require'           => '请输入银行卡城市',
            'real_name.require'           => '请输入真实姓名',
        ]);


        $map = ['uuid' => $this->uuid];
        $address = $this->app->db->name($this->table)->where($map)->find();
        if (empty($address)) {
            $data['id'] = $this->app->db->name($this->table)->insertGetId($data);
        } else {
            $data['id'] = $address['id'];
            $this->app->db->name($this->table)->where($map)->update($data);
        }

        $this->success('银行卡保存成功！', $this->_getAddress($data['id']));
    }

    /**
     * 获取银行卡详情
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function detail()
    {
        $query = $this->app->db->name($this->table)->withoutField('deleted');
        $query->where(['uuid' => $this->uuid, 'deleted' => 0, 'status' => 1]);
        $result = $query->findOrEmpty();
        $this->success('获取银行卡数据！', $result ?: null);
    }


    /**
     * 获取银行卡
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function get()
    {
        $query = $this->_query($this->table)->withoutField('deleted');
        $query->equal('id')->where(['uuid' => $this->uuid, 'deleted' => 0, 'status' => 1]);
        $result = $query->order('id desc')->page(true, false, false, input('limit', 15));
        $this->success('获取银行卡数据！', $result);
    }

    /**
     * 修改银行卡状态
     * @throws \think\db\exception\DbException
     */
    public function state()
    {
        $data = $this->_vali([
            'uuid.value'   => $this->uuid,
            'type.in:0,1'  => '银行卡状态不在范围！',
            'type.require' => '银行卡状态不能为空！',
            'id.require' => '银行卡编号不能为空！',
        ]);
        // 检查银行卡是否存在
        $map = ['uuid' => $data['uuid'], 'id' => $data['id']];
        if ($this->app->db->name($this->table)->where($map)->count() < 1) {
            $this->error('修改的银行卡不存在！');
        }
        // 更新默认银行卡状态
        $data['type'] = intval($data['type']);
        $this->app->db->name($this->table)->where($map)->update(['type' => $data['type']]);
        // 去除其它默认选项
        if ($data['type'] > 0) {
            $map = [['uuid', '=', $this->uuid], ['id', '<>', $data['id']]];
            $this->app->db->name($this->table)->where($map)->update(['type' => 0]);
        }
        $this->success('默认设置成功！', $this->_getAddress($data['id']));
    }

    /**
     * 删除银行卡
     * @throws \think\db\exception\DbException
     */
    public function remove()
    {
        $map = $this->_vali([
            'uuid.value' => $this->uuid, 'id.require' => '银行卡ID不能为空！',
        ]);
        $address = $this->app->db->name($this->table)->where($map)->find();
        if (empty($address)) $this->error('需要删除的银行卡不存在！');
        if ($this->app->db->name($this->table)->where($map)->update(['deleted' => 1]) !== false) {
            $this->success('删除银行卡成功！');
        } else {
            $this->error('删除银行卡失败！');
        }
    }

    /**
     * 获取指定的银行卡
     * @param $id
     * @return null|array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    private function _getAddress($id): ?array
    {
        $map = ['id' => $id, 'uuid' => $this->uuid, 'deleted' => 0, 'status' => 1];
        return $this->app->db->name($this->table)->withoutField('deleted')->where($map)->find();
    }

}